'''
模拟人人网的登陆，并爬取登陆后主页的个人信息，使用了cookiejar的方式来存放浏览器的cookie数据
'''

from urllib import request,parse
import re,gzip,time

# cookie管理模块,
from http import cookiejar
# 返回存储cookie对象
cookie = cookiejar.CookieJar()
# 返回一个cookie管理器
cookie_handler = request.HTTPCookieProcessor(cookie)
# 请求管理器
opener = request.build_opener(cookie_handler)

#模拟登陆
def doLogin():
    login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2018321648829'
    data = {
        'email':'1352*****16',
        'icode':'',
        'origURL':'http://www.renren.com/home',
        'domain':'renren.com',
        'key_id':'1',
        'captcha_type':'web_login',
        'password':'478b7c2dca554eeabed3b7374703bff4a6a22e78b8a9fcfb090e3a7fb792992b',
        'rkey':'e954ec64a7ecf4e33bdf81bb1abad158',
        'f':'http%3A%2F%2Fwww.renren.com%2F965541786',
    }
    data = parse.urlencode(data)

    headers = {
        'Content-Length' : len(data)
    }
    req = request.Request(url=login_url,data=bytes(data,encoding='utf-8'),headers=headers,)
    response = opener.open(req)

#访问主页个人信息
def myHome():
    home_url = 'http://www.renren.com/965541786'
    res = opener.open(home_url)
    html = res.read().decode("utf-8") 
    #print(html)
    print(re.findall("<title>(.*?)</title>",html))

if __name__ == '__main__':
    # 登陆
    print("正在登录中...")
    doLogin()
    time.sleep(3)
    # 访问个人首页
    myHome()


'''
使用requests实现人人网登录并抓取登录后信息
'''
import requests
import re,time

s = requests.Session()

def doLogin():
    login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2018321648829'
    data = {
        'email':'1352*****6',
        'icode':'',
        'origURL':'http://www.renren.com/home',
        'domain':'renren.com',
        'key_id':'1',
        'captcha_type':'web_login',
        'password':'478b7c2dca554eeabed3b7374703bff4a6a22e78b8a9fcfb090e3a7fb792992b',
        'rkey':'e954ec64a7ecf4e33bdf81bb1abad158',
        'f':'http%3A%2F%2Fwww.renren.com%2F965541786',
    }

    s.post(login_url,data=data)

def myHome():
    home_url = 'http://www.renren.com/965541786'
    res = s.get(home_url)
    html = res.content.decode("utf-8") 
    #print(html)
    print(re.findall("<title>(.*?)</title>",html))

if __name__ == '__main__':
    # 登陆
    print("正在登录中...")
    doLogin()
    time.sleep(3)
    # 访问个人首页
    myHome()